package com.jintian.smart.kernel.flowable.listener;

import com.jintian.smart.kernel.core.flow.FlowAction;
import com.jintian.smart.kernel.core.flow.IFlowEventListener;
import com.jintian.smart.kernel.core.flow.IProcessContext;
import com.jintian.smart.kernel.flowable.context.DefaultProcessContext;
import org.flowable.task.api.TaskInfo;

import java.util.List;

public class AbstractFlowEventListener implements IFlowEventListener<List<TaskInfo>> {

    private ThreadLocal<IProcessContext<List<TaskInfo>>> threadProcessContext = new ThreadLocal<>();

    @Override
    public void bindProcessContext(IProcessContext<List<TaskInfo>> context) {
        this.threadProcessContext.set(context);
    }

    public void unbindProcessContext() {
        this.threadProcessContext.set(null);
    }

    @Override
    public DefaultProcessContext getProcessContext() {
        return (DefaultProcessContext) this.threadProcessContext.get();
    }

    public <T> T getBizBean(Class<T> clazz) {
        return clazz.cast(this.getProcessContext().getTransientVariables().get(FlowAction.TRANSIENT_VARNAME_BIZBEAN));
    }

    @Override
    public void before(IProcessContext<List<TaskInfo>> context) {
        switch (context.getAction()) {
            case delete:
                this.beforeDelete(context);
                break;
            case abort:
                this.beforeAbort(context);
                break;
            case abortTask:
                this.beforeAbortTask(context);
                break;
            case claim:
                this.beforeClaimTask(context);
                break;
            case complete:
                this.beforeCompleteTask(context);
                break;
            case fallback:
                this.beforeFallbackTask(context);
                break;
            case resume:
                this.beforeResume(context);
                break;
            case start:
                this.beforeStart(context);
                break;
            case suspend:
                this.beforeSuspend(context);
                break;
            case transform:
                this.beforeTransform(context);
                break;
            case unclaim:
                this.beforeUnclaimTask(context);
                break;
            case withdraw:
                this.beforeWithdraw(context);
                break;
            case query:
                this.beforeQuery(context);
                break;
            default:
                break;
        }
    }

    @Override
    public void after(IProcessContext<List<TaskInfo>> context) {
        switch (context.getAction()) {
            case delete:
                this.afterDelete(context);
                break;
            case abort:
                this.afterAbort(context);
                break;
            case abortTask:
                this.afterAbortTask(context);
                break;
            case claim:
                this.afterClaimTask(context);
                break;
            case complete:
                this.afterCompleteTask(context);
                if (context.isProcessFinished()) {
                    this.beforeFinish(context);
                    this.afterFinish(context);
                }
                break;
            case fallback:
                this.afterFallbackTask(context);
                break;
            case resume:
                this.afterResume(context);
                break;
            case start:
                this.afterStart(context);
                break;
            case suspend:
                this.afterSuspend(context);
                break;
            case transform:
                this.afterTransform(context);
                break;
            case unclaim:
                this.afterUnclaimTask(context);
                break;
            case withdraw:
                this.afterWithdraw(context);
                break;
            case query:
                this.afterQuery(context);
                break;
            default:
                break;
        }

    }

    // ================================before===========================
    protected void beforeDelete(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeAbort(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeAbortTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeCompleteTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeFallbackTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeClaimTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeUnclaimTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeStart(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeResume(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeSuspend(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeTransform(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeWithdraw(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeFinish(IProcessContext<List<TaskInfo>> context) {

    }

    protected void beforeQuery(IProcessContext<List<TaskInfo>> context) {

    }

    // ================================after===========================

    protected void afterDelete(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterAbort(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterAbortTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterCompleteTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterFallbackTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterClaimTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterUnclaimTask(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterStart(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterResume(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterSuspend(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterTransform(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterWithdraw(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterFinish(IProcessContext<List<TaskInfo>> context) {

    }

    protected void afterQuery(IProcessContext<List<TaskInfo>> context) {

    }
}
